package repository

import (
	"good.house/internal/dto/request"
	"good.house/internal/model"
	"gorm.io/gorm"
)

type IList interface {
	FindRents(req *request.ListRequest) ([]model.Publish, error)
}

type List struct {
	db *gorm.DB
}

func NewListRepository(db *gorm.DB) *List {
	return &List{
		db: db,
	}
}

func (list *List) FindRents(req *request.ListRequest) ([]model.Publish, error) {
	var publish = []model.Publish{}
	var res *gorm.DB
	if req.ID == 0 {
		res = list.db.Order("id asc").Limit(req.PageSize).Find(&publish)
	} else {
		res = list.db.Order("id asc").Limit(req.PageSize).Where("id > ?", req.ID).Find(&publish)
	}

	if res.Error != nil {
		return nil, res.Error
	} else {
		return publish, nil
	}
}
